package com.project.web.controller.sys;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.project.web.controller.BaseController;
import com.project.entity.sys.Department;
import com.project.service.sys.DepartmentService;
import com.project.util.JacksonUtil;

/**
 * DeptController
 * @author weimingsen
 *
 */
@Controller
@RequestMapping("/dept")
public class DeptController extends BaseController<Department>{
	
	private static final Logger logger = LoggerFactory.getLogger(DeptController.class);
	
	@Autowired
	private DepartmentService departmentService;
	
	/**
	 * 查看部门
	 * @param model
	 * @param request
	 * @param department
	 * @return
	 */
	@RequestMapping(value="list")
	@RequiresPermissions("dept:list")
	public String list(Model model,HttpServletRequest request,@ModelAttribute Department department)throws Exception{
		
		String deptTree = departmentService.queryList();
		
		model.addAttribute("deptTree", deptTree);
		return "/dept/dept";
	}	
	
	/**
	 * 查看部门
	 * @param model
	 * @param request
	 * @param user
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="viewDept")
	@RequiresPermissions("dept:view")
	@ResponseBody
	public String viewDept(Model model,HttpServletRequest request)throws Exception{
		String id = request.getParameter("id");
		
		Department department = departmentService.getById(Long.parseLong(id));
		
		return JacksonUtil.objToJson(department);
	}
	
	/**
	 * 校验部门名称是否重复
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="repetitionValidate")
	@ResponseBody
	public String repetitionValidate(Model model,HttpServletRequest request){
		String name = request.getParameter("name");
		String status = request.getParameter("status");
		String updateId = request.getParameter("updateId");
		
		String flag = departmentService.getStatus(name, status, updateId);
		
		return flag;
	}
	
	/**
	 * 新增下级部门
	 * @param entity
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="add")
	@RequiresPermissions("dept:add")
	@ResponseBody
	public String add(@ModelAttribute(value="entity") Department entity, Model model,HttpServletRequest request){
		String deptId = request.getParameter("deptId");
		departmentService.addDept(entity, deptId);
		return "true";
	}
	
	/**
	 * 修改部门
	 * @param entity
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="update")
	@RequiresPermissions("dept:update")
	@ResponseBody
	public String update(@ModelAttribute(value="entity") Department entity, Model model,HttpServletRequest request){
		try {
			departmentService.updateDept(entity);
		} catch (DataIntegrityViolationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "DataIntegrityViolation";
		}
		return "true";
	}
	
	/**
	 * 校验是否可以删除
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="whetherCanDelete")
	@ResponseBody
	public String whetherCanDelete(@ModelAttribute(value="entity") Department entity,Model model,HttpServletRequest request){
		return departmentService.whetherCanDelete(entity.getCode());
	}
	
	/**
	 * 删除部门
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="delete")
	@RequiresPermissions("dept:delete")
	@ResponseBody
	public String delete(@ModelAttribute(value="entity") Department entity, Model model,HttpServletRequest request){
		if("91qq".equals(entity.getName())){
			return "root";
		}
		
		departmentService.delete(entity);
		return "true";
	}
	
	
	
	@Override
	protected Class<Department> getClazz() {
		return Department.class;
	}
	
}
